home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / appleevent send and receive / source / aerecieve source / aereceive.java next >
Encoding:
Java Source  |  2000-06-23  |  6.9 KB  |  201 lines

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.AWTEventMulticaster;
  4.  
  5. import  com.apple.mrj.jdirect.PointerStruct;
  6.  
  7. /**
  8.  * Apple Worldwide Developer Technical Support
  9.  *
  10.  * Sample showing how to send and receive AppleEvents using JDirect 2.
  11.  *
  12.  * File: AERecive.java
  13.  *
  14.  * This class contains the code needed to register an AppleEvent handler
  15.  * to recive and reply to AppleEvents.
  16.  * The received AppleEvent's text data is fired as an java.awt.event.ActionEvent
  17.  * to all registered ActionListeners.
  18.  *
  19.  * @author Levi Brown
  20.  * @author Michael Hopkins
  21.  * @author Apple Computer, Inc.
  22.  *
  23.  * Copyright ©1999 Apple Computer, Inc.
  24.  * All rights reserved.
  25.  *
  26.  * @version 1.0
  27.  * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
  28.  *
  29.  * You may incorporate this sample code into your applications without
  30.  * restriction, though the sample code has been provided "AS IS" and the
  31.  * responsibility for its operation is 100% yours.  However, what you are
  32.  * not permitted to do is to redistribute the source as "Apple Sample
  33.  * Code" after having made changes. If you're going to re-distribute the
  34.  * source, we require that you make it clear in the source that the code
  35.  * was descended from Apple Sample Code, but that you've made changes.
  36.  */
  37. public class AEReceive implements TypesConstants, AppleEventConstants, AEDataModelConstants
  38. {
  39.     /**
  40.      * The maximum length of the keyDirectObject parameter of the Apple Event
  41.      */
  42.     protected static final int kMaxTextSize    = 255;
  43.     /**
  44.      * The OSType (four character code) of the event class.
  45.      * 'Test' in this case.
  46.      */
  47.     protected static final int kTestClass    = 0x54657374; //'Test'
  48.     /**
  49.      * The OSType (four character code) of the event ID.
  50.      * 'TsID' in this case.
  51.      */
  52.     protected static final int kTestID        = 0x54734944; //'TsID'
  53.     /**
  54.      * The Universal Proceedure Pointer for the Apple Event handler.
  55.      * We keep a reference to this object to prevent the garbage collector
  56.      * from disposing it, to prevent potential Mixed Mode Manager execution failures.
  57.      * @see #AEInstallHandlers
  58.      */
  59.     protected AEEventHandlerClosureUPP myAEHandlerUPP;
  60.     /**
  61.      * The list of all registered ActionEvent listeners for this object.
  62.      * @see #addActionListener
  63.      * @see #removeActionListener
  64.      * @see #fireActionEvent
  65.      */
  66.     protected ActionListener actionListener = null;
  67.  
  68.     /**
  69.      * Creates a new AERecive object and initializes defaults.
  70.      */
  71.     public AEReceive()
  72.     {
  73.     }
  74.     
  75.     /**
  76.      * Installs our AppleEvent handler for our supported high-level event.
  77.      * @exception if any problem occured while registering the AppleEvent handler.
  78.      */    
  79.     public void installAEHandler() throws NativeException
  80.     {
  81.         short err;
  82.  
  83.         //Create our Universal Proceedure Pointer to reference our callback method.
  84.         myAEHandlerUPP = new AEEventHandlerClosureUPP(new MyAEHandler());
  85.         
  86.         //Install our Apple Event handler.
  87.         err = AppleEventFunctions.AEInstallEventHandler( kTestClass, kTestID, myAEHandlerUPP, 0, false );
  88.         ErrorHandler.checkError(err, "AEInstallHandlers: Error returned from AEGetParamPtr");
  89.     }
  90.  
  91.     /**
  92.      * A wrapper class to implement the AEEventHandlerInterface for our callback routine.
  93.      */
  94.     public class MyAEHandler implements AEEventHandlerInterface
  95.     {
  96.         /**
  97.          * The method <CODE>AEEventHandler</CODE> will be invoked via a <CODE>AEEventHandlerClosureUPP</CODE> thunk.<BR>
  98.          * No conversion is done to wrap java objects around the raw native parameters.<BR> 
  99.          * You may need to do the conversion yourself. 
  100.          * 
  101.          * @param theAppleEvent    in C: <CODE>const AppleEvent *theAppleEvent</CODE>
  102.          * @param reply            in C: <CODE>AppleEvent *reply</CODE>
  103.          * @param handlerRefcon    in C: <CODE>UInt32 handlerRefcon</CODE>
  104.          * @return                in C: <CODE>OSErr </CODE>
  105.          */
  106.         public short AEEventHandler(int theAppleEvent, int reply, int handlerRefcon)
  107.         {
  108.             int[]    actualType = new int[1];
  109.             int[]    actualSize = new int[1];
  110.             byte[]    theText = new byte[kMaxTextSize];
  111.             short    err;
  112.             
  113.             try
  114.             {
  115.                 //Here we are creating a anonymous class which implements the abstracted getSize menthod of the PointerStruct class,
  116.                 //in order to create a PointerStruct to fill with the AppleEvent.
  117.                 PointerStruct eventPtr = new PointerStruct(theAppleEvent)
  118.                                         {
  119.                                             public int getSize()
  120.                                             {
  121.                                                 return AppleEventStruct.sizeOfAppleEvent;
  122.                                             }
  123.                                         };
  124.  
  125.                 //Create a new AppleEventStruct using the constructor to copy data received by our handler.
  126.                 AppleEventStruct aeStruct = new AppleEventStruct(eventPtr, 0);
  127.                 
  128.                 //We are using AEGetParamPointer to retrieve our data (text) from the Apple Event, which gets stored in 'theText'.
  129.                 err = AEDataModelFunctions.AEGetParamPtr(aeStruct, keyDirectObject, typeChar, actualType, theText, kMaxTextSize, actualSize);
  130.                 ErrorHandler.checkError(err, "AEEventHandler: Error returned from AEGetParamPtr");
  131.  
  132.                 //Create a String from the byte array of returned data.
  133.                 String text = new String(theText, 0, actualSize[0]);
  134.                 
  135.                 //Fire an ActionEvent with the data so registered ActionListeners can see the AppleEvent being received.
  136.                 fireActionEvent(text);
  137.                 
  138.                 //Here we are creating another anonymous class which implements the abstracted getSize menthod of the PointerStruct class.
  139.                 //In this case it is for the reply.
  140.                 PointerStruct replyPtr = new PointerStruct(reply)
  141.                                         {
  142.                                             public int getSize()
  143.                                             {
  144.                                                 return AppleEventStruct.sizeOfAppleEvent;
  145.                                             }
  146.                                         };
  147.  
  148.                 //Create a new AppleEventStruct using the constructor to copy the reply.
  149.                 AppleEventStruct aeReplyStruct = new AppleEventStruct(replyPtr, 0);
  150.  
  151.                 //Make a reply
  152.                 String replyText = "Thank you sir, may I have another?!";
  153.             
  154.                 //Turn the reply String into a byte array
  155.                 byte[] theData = replyText.getBytes();
  156.                 
  157.                 //Put the string into the direct object parameter
  158.                 err = AEDataModelFunctions.AEPutParamPtr(aeReplyStruct, keyDirectObject, typeChar, theData, theData.length);
  159.                 ErrorHandler.checkError(err, "AEEventHandler: Error returned from AEPutParamPtr");
  160.             }
  161.             catch (NativeException exc)
  162.             {
  163.                 System.err.println(exc.getMessage());
  164.                 return (exc.getErrNum());
  165.             }
  166.  
  167.             return noErr;
  168.         }
  169.     }
  170.     
  171.     /**
  172.      * Adds the specified action listener to receive action events from this object.
  173.      * @param l the action listener
  174.      */
  175.     public void addActionListener(ActionListener l)
  176.     {
  177.         actionListener = AWTEventMulticaster.add(actionListener, l);
  178.     }
  179.  
  180.     /**
  181.      * Removes the specified action listener so it no longer receives
  182.      * action events from this object.
  183.      * @param l the action listener
  184.      */
  185.     public void removeActionListener(ActionListener l)
  186.     {
  187.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  188.     }
  189.  
  190.     /**
  191.      * Fire an action event to the listeners.
  192.      * @param the action command associated with the event.
  193.      * In this case it will be the text from the AppleEvent.
  194.      */
  195.     protected void fireActionEvent(String message)
  196.     {
  197.         if (actionListener != null)
  198.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, message));
  199.     }
  200. }
  201.